# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=installer
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
pkgver=0.7.0
pkgrel=4
pkgdesc="A low-level library for installing from a Python wheel distribution (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_references=(
  'purl: pkg:pypi/installer'
)
url='https://github.com/pradyunsg/installer'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-flit-core"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "git"
)
_launcher_commit="8c455825fc1308d34a1a47238a9eb6d56f732142"
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "simple_launcher"::"git+https://bitbucket.org/vinay.sajip/simple_launcher.git#commit=${_launcher_commit}"
        "001-launcher-relative.patch"
        "002-launcher-stderr-missing.patch"
        "003-launcher-secure-api-shim.patch"
        "004-zero-reservered-fields-in-startupinfo.patch")
sha256sums=('a26d3e3116289bb08216e0d0f7d925fcef0b0194eedfa0c944bcaaa106c4b631'
            'SKIP'
            'c83360c3ffca0b278b05841a3a39b784df42a4d5fbaf2823c2a7e3f32fe8ebc4'
            'e95b68c14bffddd6dd859bdb2c8feb733bbcd5c9d4a37105df4196bfa01923c5'
            '0b6ab782f1550ea0e52a8869cf9c78c405a9ed9dda1e9d9eb60a4126787db208'
            'ccb341afa03809257d0c9c6c5d323b9998d21ff972789eb8c1734d5bc2a7fbff')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}/001-launcher-relative.patch"

  cd "${srcdir}/simple_launcher"

  # https://github.com/pypa/pip/issues/10444#issuecomment-1031092176
  patch -Np1 -i "${srcdir}/002-launcher-stderr-missing.patch"

  patch -Np1 -i "${srcdir}/003-launcher-secure-api-shim.patch"

  patch -Np1 -i "${srcdir}/004-zero-reservered-fields-in-startupinfo.patch"
}

build() {
  cd "${srcdir}/simple_launcher"
  windres --input launcher.rc --output resources.res --output-format=coff

  local _gui_cflags="$CFLAGS $CPPFLAGS -mwindows"
  local _cli_cflags="$CFLAGS $CPPFLAGS -D_CONSOLE"
  local _gui_lflags="$LDFLAGS -lshlwapi"
  local _cli_lflags="$LDFLAGS -lshlwapi"

  case "${MINGW_CHOST}" in
  i686-w64-mingw32)
    cc $_cli_cflags -o t32.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w32.exe launcher.c resources.res $_gui_lflags
    ;;
  x86_64-w64-mingw32)
    cc $_cli_cflags -o t64.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w64.exe launcher.c resources.res $_gui_lflags
    ;;
  aarch64-w64-mingw32)
    cc $_cli_cflags -o t64-arm.exe launcher.c resources.res $_cli_lflags
    cc $_gui_cflags -o w64-arm.exe launcher.c resources.res $_gui_lflags
    ;;
  *)
    echo "Unsupported CHOST ${MINGW_CHOST}" && false
    ;;
  esac

  cd "${srcdir}/${_realname}-${pkgver}"

  rm src/installer/_scripts/*.exe
  cp "${srcdir}/simple_launcher/"*.exe src/installer/_scripts/
  # git add src/installer/_scripts/

  python -m flit_core.wheel
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  PYTHONPATH="${srcdir}/${_realname}-${pkgver}/src" \
    python -m installer --prefix=${MINGW_PREFIX} --destdir="${pkgdir}" dist/*.whl
}
